﻿using AutoMapper;
using Factory.EquipmentManagement.CheckMaintenanceItems.Dto;
using Factory.MasterData.Materials.Dto;
using Factory.MasterData.Vendors.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.EquipmentManagement.CheckMaintenanceItems
{
    /// <summary>
    /// 点检保养项目服务
    /// </summary>
    public class CheckMaintenanceItemAppService : ApplicationService, ICheckMaintenanceItemAppService
    {
        /// <summary>
        /// 点检保养项目仓储接口
        /// </summary>
        private readonly IRepository<CheckMaintenanceItem, int> _checkMaintenanceItemrepository;
        /// <summary>
        /// 仓储接口
        /// </summary>
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="checkMaintenanceItemrepository">点检保养项目仓储接口</param>
        /// <param name="mapper">仓储接口</param>
        public CheckMaintenanceItemAppService(IRepository<CheckMaintenanceItem, int> checkMaintenanceItemrepository, IMapper mapper)
        {
            _checkMaintenanceItemrepository = checkMaintenanceItemrepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加重复性验证
        /// </summary>
        /// <param name="itemCode">点检保养项目编码</param>
        /// <returns>返回结果</returns>
        public int AddRepeatability(string itemCode)
        {
            long count = _checkMaintenanceItemrepository.GetListAsync(m => m.ItemCode == itemCode).Result.Count();
            return count > 0 ? -1 : 0;
        }
        /// <summary>
        /// 添加点检保养项目服务
        /// </summary>
        /// <param name="checkMaintenanceItemDto">点检保养项目服务Dto</param>
        /// <returns>返回结果</returns>
        public async Task<int> Insert(CheckMaintenanceItemDto checkMaintenanceItemDto)
        {
            var reslut = AddRepeatability(checkMaintenanceItemDto.ItemCode);
            if (reslut != 0) return -1;
            CheckMaintenanceItem checkMaintenanceItem =_mapper.Map<CheckMaintenanceItem>(checkMaintenanceItemDto);
            await _checkMaintenanceItemrepository.InsertAsync(checkMaintenanceItem);
            return 1;
        }
        /// <summary>
        /// 分页显示点检保养项目服务
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="itemCode">项目编号</param>
        /// <returns>返回结果</returns>
        public async Task<PageResult> PageList(int pageIndex, int pageSize, string? itemCode)
        {
            var list =await _checkMaintenanceItemrepository.GetListAsync(m =>string.IsNullOrEmpty(itemCode) || m.ItemCode.Contains(itemCode));
            PageResult pageResult = new PageResult();
            pageResult.RowCount = list.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }
        /// <summary>
        /// 删除点检保养项目服务
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回结果</returns>
        public async Task<int> Deleted(string id)
        {
            var ids = id.Split(',');
            foreach (var item in ids)
            {
                await _checkMaintenanceItemrepository.DeleteAsync(m => m.Id == Convert.ToInt32(item));
            }
            return 1;
        }
        /// <summary>
        /// 编辑重复性验证
        /// </summary>
        /// <param name="itemCode">点检保养项目编码</param>
        /// <param name="id">主键</param>
        /// <returns>返回结果</returns>
        public async Task<int> UpdateRepeatability(string itemCode,int id)
        {
            var list =await _checkMaintenanceItemrepository.GetListAsync(m => m.ItemCode == itemCode&&m.Id!=id);
            return list.Count() > 0 ? -1 : 0;
        }
        /// <summary>
        /// 编辑点检保养项目服务
        /// </summary>
        /// <param name="checkMaintenanceItemDto">点检保养项目服务Dto</param>
        /// <returns>返回结果</returns>
        public async Task<int> Update(CheckMaintenanceItemDto checkMaintenanceItemDto)
        {
            CheckMaintenanceItem checkMaintenanceItem = _mapper.Map<CheckMaintenanceItem>(checkMaintenanceItemDto);
            await _checkMaintenanceItemrepository.UpdateAsync(checkMaintenanceItem);
            return 1;
        }
    }
}
